package com.negative.logic;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import com.negative.activities.R;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.PopupMenu;

public class MenuListener implements OnClickListener{

	Context context;
	
	public MenuListener(Context context) {
		this.context = context;
	}
	
	@Override
	public void onClick(View v) {
		//Creating the instance of PopupMenu  
        PopupMenu popup = new PopupMenu(context, v);  
        //Inflating the Popup using xml file  
        popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
        popup.setOnMenuItemClickListener(new MenuItemListener(context));
        try {
            Field[] fields = popup.getClass().getDeclaredFields();
            for (Field field : fields) {
                if ("mPopup".equals(field.getName())) {
                    field.setAccessible(true);
                    Object menuPopupHelper = field.get(popup);
                    Class<?> classPopupHelper = Class.forName(menuPopupHelper
                            .getClass().getName());
                    Method setForceIcons = classPopupHelper.getMethod(
                            "setForceShowIcon", boolean.class);
                    setForceIcons.invoke(menuPopupHelper, true);
                    break;
                }
            }
        } catch (Exception e) {
            System.out.println("Can't show icons on popup menu...");
        }
        popup.show();
	}

}
